<?php

namespace V1\Model;


use Think\Exception;
use Think\Model;

class SelectedCategoryModel extends Model
{
    protected $tableName = 'selected_category';


    /**
     * 获取关注的圈子列表
     * @param $userid [用户id]
     * @return mixed
     * @throws Exception
     */
    public function get_my_category_list($userid)
    {
        $request = D('SelectedCategory')
            ->alias('s')
            ->join('__CATEGORY__ c ON s.cat_id = c.id')
            ->where('s.uid=' . $userid)
            ->field('s.cat_id,c.cat_name,c.cat_img')
            ->select();
        if($request===false){
            throw new Exception(C('STATUS')['101']);
        }else{
            return $request;
        }
    }

    /**
     * 获取热门分类列表
     * @return mixed
     */
    public function get_category_list()
    {
        $category = new CategoryModel();
            $data = $category->get_category_list(null, null, null, null, 2);
        return $data;
    }

    /**
     * 查询是否添加过产品分类
     * @param null $cat_id
     * @param null $userid
     * @return int|mixed
     */
    public function get_my_category($cat_id = null, $userid = null)
    {
        if (empty($userid)) {
            return 0;
        }
        $map['cat_id'] = $cat_id;
        $map = ['cat_id' => $cat_id, 'uid' => $userid];
        $request = $this->where($map)->select();
        return $request;
    }

    /**
     * 获取圈子成员数
     * @param $cat_id
     * @return mixed
     */
    public function get_community_member_count($cat_id)
    {
        $count = $this->where(array('cat_id' => $cat_id))->count();
        return $count;
    }

    /**
     * 查询是否添加过产品分类
     * @param null $cat_id
     * @param null $u_id
     * @return int|mixed
     */
    public function getMyCategory($cat_id = null, $u_id = null)
    {
        if (empty($u_id)) {
            $request = 0;
        } else {
            $map['cat_id'] = $cat_id;
            $map['uid'] = $u_id;
            $request = $this->where($map)->select();
        }
        return $request;
    }

    /**
     * 添加个人产品分类
     * @param $cat_id
     * @param $u_id
     * @return int
     */
    public function addSelectCategory($cat_id, $u_id)
    {
        $category_data = $this->getMyCategory($cat_id, $u_id);
        if (count($category_data) > 0) {
            $result['code'] = 201;
            $result['msg'] = C('STATUS')['217'];
            $result['result'] = false;
        } else {
            $data['cat_id'] = $cat_id;
            $data['uid'] = $u_id;
            try {
                $request = $this->add($data);
                if (!$request) {
                    throw new Exception();
                } else {
                    $result['code'] = 200;
                    $result['msg'] = C('STATUS')['200'];
                    $result['result'] = true;
                }
            } catch (Exception $exception) {
                $result['code'] = 400;
                $result['msg'] = C('STATUS')['101'];
                $result['result'] = false;
            }
        }
        return $result;
    }

    /**
     * 取消我的圈子关注
     * @param cat_id
     * @param $u_id
     * @return int
     */
    public function del_category($cat_id, $u_id)
    {
        if (!empty($u_id)) {
            $map['uid'] = $u_id;
            $map['cat_id'] = $cat_id;
            try {
                $request = $this->where($map)->delete();  // 删除主键为$cat_id的参数
                if (!$request) {
                    throw new Exception();
                } else {
                    $result['code'] = 200;
                    $result['msg'] = C('STATUS')['200'];
                    $result['result'] = true;
                }
            } catch (Exception $exception) {
                $result['code'] = 400;
                $result['msg'] = C('STATUS')['101'];
                $result['result'] = false;
            }
        } else {
            $result['code'] = 201;
            $result['msg'] = C('STATUS')['201'];
            $result['result'] = false;
        }
        return $result;
    }
}